<?php

/**
 * page actions.
 *
 * @package    khas
 * @subpackage page
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 3335 2007-01-23 16:19:56Z fabien $
 */
class mainActions extends sfActions
{
  
    public function executeSearch(sfWebRequest $request)
    {
        $keyword = $request->getParameter('searchinput');
        $keyword = ('Хайх утгаа оруулна уу' == ($keyword)) ? '' : $keyword;
        $this->keyword = $keyword;
        
        $this->page = $page = $request->getParameter('page');
    }
    
    
    
    
    public function preExecute()
    {
        ProjectConfiguration::getApplicationConfiguration('front', 'prod', false)->loadHelpers('Url');
    }
    
    public function executeIndex(sfWebRequest $request)
    {
        $this->getUser()->setAttribute('activeSeasonId', 'home');
    }
    
    public function executeTlg(sfWebRequest $request)
    {
       $this->getUser()->setAttribute('activeSeasonId', 'tlg');
    }
    
    public function executeDream(sfWebRequest $request)
    {
       $this->getUser()->setAttribute('activeSeasonId', 'dream');
    }
    
    public function executeNbalounge(sfWebRequest $request)
    {
       $this->getUser()->setAttribute('activeSeasonId', 'nbalounge');
    }
    
    public function executeHome(sfWebRequest $request)
    {
        $this->seasonId = $seasonId = $request->getParameter('seasonId');
        $season = Doctrine::getTable('BbSeason')->find($seasonId);
        $this->forward404Unless($season);
        $this->getUser()->setAttribute('activeSeasonId', $seasonId);
        $this->season = $season;
    }


    public function executeCulture(sfWebRequest $request)
    {
      $culture = in_array($request->getParameter('culture'), array('en', 'mn')) ? $request->getParameter('culture') : 'mn';
      $this->getUser()->setCulture($culture);
      $this->getResponse()->setCookie('culture', $culture);
      
      $this->redirect($request->getReferer() ? $request->getReferer() : '@homepage');
    }
    
    
    public function executeSubscribe(sfWebRequest $request)
    {
        if ($request->isMethod(sfRequest::POST))
        {        
          $email = $request->getParameter('email_subscribe');
          if(!$email)
          {
            echo __('Email address is required.');
            return sfView::NONE;
          }
          else
          {
            $validator = new sfValidatorEmail();
            try{ $validator->clean($email); } catch (sfValidatorError $e)
            {
              echo __('Invalid email address.');
              return sfView::NONE;
            }
          }
          
          $a = $request->getParameter('a');
          if($a == 'subscribe')
          {
              if(!Doctrine::getTable('Subscriber')->findByEmail($email))
              {
                $subscriber = new Subscriber();
                $subscriber->setEmail($email);
                $subscriber->save();
                echo __('Successful subscribed.');
              } else echo __('This email address is already exits.');
          }
          elseif($a == 'unsubscribe')
          {
              if($email = Doctrine::getTable('Subscriber')->findByEmail($email))
              {
                $email->delete();
                echo __('Successfully deleted.');
              } else echo __('This email address does not exits.');  
          }
        }
        return sfView::NONE;
    }
  
  

    public function executeContact(sfWebRequest $request)
    {
        $form  = new FeedbackForm();
  
        /* POST */
        if ($request->isMethod(sfRequest::POST))
        {
          $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    
          if ($form->isValid())
          {
              //send mail
              $mailBody = $this->getPartial("main/mailFeedback", array('name'=>$form->getValue('name'), 'email'=>$form->getValue('email'), 'body'=>$form->getValue('message')));
              $message = $this->getMailer()->compose($form->getValue('email'), 'handaa.1224@gmail.com', 'www.grandroyal.mn :: Санал хүсэлт ирлээ.', $mailBody);
              $message->setContentType("text/html");
              $this->getMailer()->send($message);
              
              $this->getUser()->setFlash('flash', __('Таны санал хүсэлт амжилттай илгээгдлээ.'), true);
            
              $this->redirect('@contact');
          }
        } // END POST
    
        $this->form = $form;
        
        return sfView::SUCCESS;
    }
  

    
    public function executeDefault(sfWebRequest $request)
    {    
      $this->getUser()->setFlash("warning", $request->getParameter('message'), true);
      
      $this->setTemplate("default");      
      
      return sfView::SUCCESS;
    }
    
    
    public function executePage(sfWebRequest $request)
    {
      $page = Doctrine::getTable('Page')->findByRoute($request->getParameter('route'));
      $this->forward404Unless($page);
      
      $page->setHits($page->getHits()+1);
      $page->save();
    
      $this->page = $page;
    }

    
    
    public function executeTmp(sfWebRequest $request)
    {
      
      
    }


}